<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <Alert v-if="loginErrorMsg" type="error" show-icon>{{loginErrorMsg}}</Alert>
                        <FormItem prop="username">
                            <Input v-model="form.username" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="verify">
                            <Row>
                                <Col span="16">
                                    <Input v-model="form.verify"  :maxlength="4" placeholder="请输入验证码">
                                    <span slot="prepend">
                                    <Icon :size="16" type="image"></Icon>
                                </span>
                                    </Input>
                                </Col>
                                <Col span="8">
                                    <img @click="verifyCodeRefresh" :style="{width:'88px', height:'33px', borderRadius:'4px'}" :src="verifyCodeUrl"/>
                                </Col>
                            </Row>


                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    import Cookies from 'js-cookie';
    import Util from '../libs/util';
    export default {
        data () {
            return {
                verifyCodeUrl: '',
                loginErrorMsg: '',
                form: {
                    username: '',
                    password: '',
                    verify: ''
                },
                rules: {
                    username: [
                        { required: true, message: '账号不能为空', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' }
                    ],
                    verify: [
                        { required: true, message: '验证码不能为空', trigger: 'blur' }
                    ]
                }
            };
        },
        mounted: function () {
            this.$nextTick(function () {
                this.verifyCodeRefresh();
            });
        },
        methods: {
            handleSubmit () {
                Cookies.remove('user');
                Cookies.remove('token');
                Cookies.remove('permissions');
                Cookies.remove('admin_type');
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        Util.ajax.post(`/adminapi/passport/login`, this.form).then(response => {
                            if(response.data.code == 0) {
                                if (!response.data.data.username) {
                                    Cookies.set('user', response.data.data.moble);
                                } else {
                                    Cookies.set('user', response.data.data.username);
                                }
                                Cookies.set('token', response.data.data.token);
                                Cookies.set('permissions', JSON.stringify(response.data.data.permissions));
                                Cookies.set('admin_type', JSON.stringify(response.data.data.admin_type));
                                console.log(Cookies.get('permissions'));
                                this.$router.push({
                                    name: 'home_index',
                                    params: {from: 'login'}
                                });
                            } else {
                                this.loginErrorMsg = response.data.msg;
                                this.verifyCodeRefresh();
                            }
                        });

                    }
                });
            },
            verifyCodeRefresh () {
                this.verifyCodeUrl = "/adminapi/passport/verify?ts=" + Math.random();
            }
        }
    };
</script>

<style>

</style>
